var formdiable = require('formidable');
var db = require('../models/db.js');
var md5 = require('../models/md5.js');
var active = 0;
const path = require('path');
const fs = require('fs');
gm = require('gm');
exports.showIndex = (req, res, next) => {
	if (req.path == '/') {
		active = 0
	}
	var avatar = 'default.jpg';
	if (req.session.login) { // 如果登陆了 数据库检索查头像
		db.find('users', {
			username: req.session.username
		}, (err, result) => {
			avatar = result[0].avatar;
			db.find('posts', {}, {
				sort: {
					"datatime": -1
				}
			}, (err, result2) => {
				res.render('index', {
					"login": req.session.login,
					"username": req.session.username,
					active: active,
					avatar: avatar,
					shuoshuo: result2
				});
			})

		})
	} else {
		res.render('index', {
			"login": req.session.login,
			"username": req.session.username,
			active: active,
			avatar: avatar,
			shuoshuo: []
		});
	}
}
exports.regist = (req, res, next) => {
	if (req.path == '/regist') {
		active = 1
	}

	res.render('regist', {
		"login": req.session.login,
		"username": req.session.username,
		active: active
	});
}

exports.doregist = (req, res, next) => {
	var form = new formdiable.IncomingForm();
	form.parse(req, (err, field, files) => {
		var username = field.username,
			password = field.password;
		db.find('users', {
			username: username
		}, (err, result) => {
			if (err) {
				res.send(-3);
				return;
			}
			if (result.length != 0) {
				res.send('-1')
				return;
			}
			db.insetOne('users', {
				username: username,
				password: md5(md5(password)),
				avatar: 'default.jpg'
			}, (err, result) => {
				if (err) {
					res.send(-3);
					return;
				}
				req.session.login = true;
				req.session.username = username;
				res.send('success');
			})
		})

	})
}

exports.showLogin = (req, res, next) => {
	if (req.path == '/login') {
		active = 2
	}
	res.render('login', {
		"login": req.session.login,
		"username": req.session.username,
		active: active
	});
}
exports.doLogin = (req, res, next) => {
	var form = new formdiable.IncomingForm();
	form.parse(req, (err, field, files) => {
		var username = field.username,
			password = field.password;
		db.find('users', {
			username: username
		}, (err, result) => {
			if (err) {
				res.send(-3);
				return;
			}
			if (result.length != 0) {
				if (result[0].password == md5(md5(password))) {
					req.session.login = true;
					req.session.username = username;
					res.send('success');
				} else {
					res.send('-2');
				}
				return;
			} else {
				res.send('-1');
				return;
			}
		})
	})
}
exports.showSetavatar = (req, res, next) => {
	if (!req.session.login) {
		res.send('非法闯入，请先登陆');
		return;
	}
	res.render('setavatar', {
		"login": true,
		"username": req.session.username,
		active: 2,
	});
}
// 接收上传
exports.dosetavatar = (req, res, next) => {
	var form = new formdiable.IncomingForm();
	form.uploadDir = path.normalize(__dirname + '/../avatar');
	form.parse(req, (err, field, files) => {
		var oldpath = files.file.path;
		var newpath = path.normalize(__dirname + '/../avatar') + '/' + req.session.username + '.jpg'
		//改名方法 注意上面的参数
		fs.rename(oldpath, newpath, (err) => {
			if (err) {
				res.send('改名失败');
				return;
			}
			req.session.avatar = req.session.username + '.jpg';
			res.redirect('/cutpic');
		})
	})
}
// 头像剪裁
exports.showcutpic = (req, res, next) => {
	if (!req.session.login) {
		res.send('非法闯入，请先登陆');
		return;
	}
	res.render('cutpic', {
		"login": true,
		"username": req.session.username,
		active: 2,
		avatar: req.session.avatar
	});
}
// 头像剪裁
exports.docut = (req, res, next) => {
	var filename = req.session.avatar;
	var w = req.query.w,
		h = req.query.h,
		x = req.query.x,
		y = req.query.y;
	// crazytown  裁剪图片
	gm('./avatar/' + filename)
		.crop(w, h, x, y) // 前两个数字为宽高，后两个坐标位置
		.resize(100, 100, '!') // '！'代表强行修改 调整尺寸
		.write('./avatar/' + filename, function (err) {
			if (err) {
				res.send('-1');
				return;
			}
			db.updateMany('users', {
				username: req.session.username
			}, {
				$set: {
					avatar: req.session.avatar
				}
			}, (err, result) => {
				if (err) {
					res.send('-1');
					return;
				}
				res.send('1');
			})

		})
}
// 说说上传
exports.doshuoshuo = (req, res, next) => {
	if (!req.session.login) {
		res.send('非法闯入，请先登陆');
		return;
	}
	var form = new formdiable.IncomingForm();
	form.parse(req, (err, field, files) => {
		var username = req.session.username,
			content = field.content;
		db.insetOne('posts', {
			username: username,
			datatime: new Date(),
			content: content
		}, (err, result) => {
			if (err) {
				res.send(-3);
				return;
			}
			res.send('1');
		})

	})
}